<template>
  <el-dialog
    :title="title"
    :visible="true"
    custom-class="dialogWrapper"
    :before-close="closeDialog">
    <el-form :model="formData" label-width="100px" ref="ruleForm" :rules="rules">
      <el-form-item label="员工姓名" prop="name">
        <el-input v-model="formData.name" auto-complete="off" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd" required>
         <el-input v-model="formData.pwd" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="verifyPwd" required>
         <el-input v-model="formData.verifyPwd" auto-complete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="info" @click="submit('ruleForm')" :disabled="Subabled">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {reset} from '@/services/staffManager'
  import {RequestError} from '@/services/errors'
  export default {
    name: 'reset-pwd',
    data: () => ({
      formData: {
        name:"",
        serialNo:"",
        pwd:"",
        verifyPwd:""
      },
      Subabled:false,
      roles: [],
      rules: {
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        verifyPwd: [
          { required: true, message: '请重新输入密码', trigger: 'blur' }
        ],
      }
    }),
    props: {
      view: {
        type: String,
        default: 'reset'
      }
    },
    computed: {
      title () {
        let content = '重置密码'
        return content
      }
    },
    methods: {
      closeDialog () {
        this.$emit('close')
      },
      submit (formName) {
      	this.Subabled = true;
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            try {
              await reset(this.formData.serialNo, this.formData.pwd,this.formData.verifyPwd)
              this.$emit('close', true)
              this.$message({
                showClose: true,
                message: '提交成功',
                type: 'success'
              })
              this.Subabled = false;
            } catch (e) {
              if (e instanceof RequestError) {
                this.$alert(e.message, '提示', {
                  type: 'error',
                  confirmButtonText: '确定'
                })
                this.Subabled = false;
              } else {
                throw e
              }
            }
          } else {
            return
          }
        })
      }
    }
  }
</script>
<style>
  .dialogWrapper {
    width: 540px;
  }
  .el-select {
    width: 100%;
  }
</style>
